//
//  MoreGoodsDetailVC.m
//  reSearchDemo
//
//  Created by 云库 on 15/9/29.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MoreGoodsDetailVC.h"
#import "UIView+Category.h"
#import "MoreGoodsDetailsCell.h"
#import "GoodsOtherBuy.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
typedef NS_ENUM(NSInteger, KJType) {
    KJIMageType,      /**< 图文详情 */
    KJParameterType   /**< 产品参数 */
};

@interface MoreGoodsDetailVC ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView *_tableView;      /**< tableView */
    NSMutableArray *_dataSource;  /**< 数据源    */
    NSMutableArray *_titleArr;    /**< 标题数据   */
}

@property (strong, nonatomic)NSMutableArray *btnArray;
@property (strong, nonatomic) UIView *firstView;
@property (strong, nonatomic) UIView *secondView;

@property (strong, nonatomic) UIView *lineView;

@property (strong, nonatomic) UICollectionView *thirdView;

/** 推荐商品数组*/
@property (strong, nonatomic) NSMutableArray *goodsArr;

@property (nonatomic,assign)KJType CellType;

@end

@implementation MoreGoodsDetailVC


- (NSMutableArray *)goodsArr{
    if (!_goodsArr) {
        _goodsArr = [NSMutableArray array];
    }
    return _goodsArr;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self AllocMenu];
    
    [self getFirstView];
    
    //商品参数
    NSDictionary *dic = @{@"id":@"19"};
    NSString *path = [NSString stringWithFormat:@"%@%@",SHOPER,@"html/agoods/spec.do"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:path parameters:dic success:^(AFHTTPRequestOperation * operation, id obj) {
        DLog(@"obj = %@",obj);
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        DLog(@"error = %@",error);
    }];
    
}
#pragma mark --初始化tableview--
- (void)allocTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 104)style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_tableView];
    
}
#pragma mark --加载数据--
- (void)loadDataSource {
    if (_CellType == KJParameterType) {
        [_dataSource removeAllObjects];
        _dataSource = nil;
        _dataSource = [NSMutableArray array];
        [_dataSource addObject:@"2015年初"];
        [_dataSource addObject:@"中"];
        [_dataSource addObject:@"全新"];
        [_dataSource addObject:@"服饰/用品"];
        [_dataSource addObject:@"上班"];
        
        [_titleArr removeAllObjects];
        _titleArr = nil;
        _titleArr = [NSMutableArray array];
        [_titleArr addObject:@"上市时间"];
        [_titleArr addObject:@"规格"];
        [_titleArr addObject:@"款型"];
        [_titleArr addObject:@"服装类型"];
        [_titleArr addObject:@"适用场合"];
    }else {
        [_dataSource removeAllObjects];
        _dataSource = nil;
        _dataSource = [NSMutableArray arrayWithArray:self.ImgArr];
    }
    [_tableView reloadData];
}

#pragma mark --初始化菜单
-(void)AllocMenu {
    self.btnArray = [NSMutableArray array];
    NSArray *titleArr = @[@"图文详情",@"详细参数",@"商家推荐"];
    for (int i = 0; i<titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(CGRectGetWidth(self.view.frame)/3*i, 64, CGRectGetWidth(self.view.frame)/3, 44);
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setTitle:titleArr[i] forState:0];
        btn.tag = i;
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(MenuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            btn.selected = YES;
        }
        [self.btnArray addObject:btn];
        [self.view addSubview:btn];
        
    }
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 104, CGRectGetWidth(self.view.frame)/3 - 20, 2)];
    self.lineView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.lineView];
}




#pragma mark --菜单按钮响应函数--
-(void)MenuBtnAction:(UIButton *)sender {
    
    int index = 0;
    for (int i = 0; i<self.btnArray.count; i++) {
        UIButton *btn = self.btnArray[i];
        btn.tag = i;
        btn.selected = NO;
        if (sender == self.btnArray[i]) {
            index = i;
        }
    }
    [UIView animateWithDuration:0.2 animations:^{
        sender.selected = YES;
        self.lineView.frame = CGRectMake(CGRectGetWidth(self.view.frame)/3*index + 10, 104, CGRectGetWidth(self.view.frame)/3 - 20, 2);
    } completion:^(BOOL finished) {
        //初始化数据
        
    }];
    
    if (sender.tag==0) {
        
        self.secondView.hidden=YES;
        self.firstView.hidden = NO;
        self.thirdView.hidden = YES;
        _tableView.hidden = NO;
        [self getFirstView];

        
    }else if (sender.tag==1){
        
        self.firstView.hidden = YES;
        self.thirdView.hidden = YES;
        _tableView.hidden = NO;
        [self getSecondView];
    }else{
        
        //获取数据
        [self getThirdViewData];
        
        self.firstView.hidden = YES;
        self.secondView.hidden = YES;
        _tableView.hidden = YES;
        if (self.thirdView==nil) {
            [self getThirdView];
        }else{
            
            self.thirdView.hidden = NO;
        }
    }
    
}

- (void)getThirdViewData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    NSString *path = [NSString stringWithFormat:@"%@%@",SHOPER,@"html/agoods/otherbuy.do?id=510600"];
    [manager GET:path parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self.goodsArr removeAllObjects];
        
        NSArray *array = [GoodsOtherBuy objectArrayWithKeyValuesArray:responseObject[@"data"]];
        
        
        [self.goodsArr addObjectsFromArray:array];
        
        [self.thirdView reloadData];
        
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             
         }];
}

#pragma mark --图文详情--
- (void)getFirstView{
//    self.firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 64+44, self.view.width, self.view.height - 108)];
//    self.firstView.backgroundColor = [UIColor lightGrayColor];
//    [self.view addSubview:self.firstView];
//    
//    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100,100, 200, 200)];
//    lable.text = @"没有数据";
//    lable.font = [UIFont boldSystemFontOfSize:25];
//    lable.textAlignment = NSTextAlignmentCenter;
//    [self.firstView addSubview:lable];
    _CellType = KJIMageType;
    [self loadDataSource];
    if (!_tableView) {
        [self allocTableView];
    }
}

#pragma mark --详细参数--
- (void)getSecondView{
//    self.secondView = [[UIView alloc] initWithFrame:CGRectMake(0, 64+44-2, self.view.width, self.view.height)];
//    self.secondView.backgroundColor = [UIColor orangeColor];
//    [self.view addSubview:self.secondView];
//    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100,100, 200, 200)];
//    lable.font = [UIFont boldSystemFontOfSize:25];
//    lable.textAlignment = NSTextAlignmentCenter;
//    lable.text = @"参数详情";
//    [self.secondView addSubview:lable];
    _CellType = KJParameterType;
    [self loadDataSource];
    if (!_tableView) {
        [self allocTableView];
    }
}


#pragma mark --商家推荐--
- (void)getThirdView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.width/2-20,70+self.view.width/2-20);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;


    self.thirdView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64+44-2, self.view.width, self.view.height-106) collectionViewLayout:layout];
    self.thirdView.backgroundColor = [UIColor lightGrayColor];
    self.thirdView.delegate = self;
    self.thirdView.dataSource =self;
    
    static NSString *identify = @"cell";
    [self.thirdView registerClass:[MoreGoodsDetailsCell class]forCellWithReuseIdentifier:identify];
    
    [self.view addSubview:self.thirdView];
    
    
    
}
#pragma mark -
#pragma mark --UICollectionViewDelegate--
//设置分区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{
    
    return 1;
}

//每个分区上的元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.goodsArr.count;
}

//设置元素内容
- (UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MoreGoodsDetailsCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.goods = self.goodsArr[indexPath.row];
    
    [cell sizeToFit];
    
    //cell.backgroundColor =[UIColor lightGrayColor];
    
    return cell;
}

//点击元素触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"%@",indexPath);
    
}

#pragma mark -
#pragma mark --tableViewDelegate--
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentifier];
    }
    if (_CellType == KJParameterType) {
        for (id obj in [cell subviews]) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *image = (UIImageView *)obj;
                [image removeFromSuperview];
            }
        }
        cell.textLabel.text = _titleArr[indexPath.row];
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.detailTextLabel.text = _dataSource[indexPath.row];
        cell.detailTextLabel.textAlignment = NSTextAlignmentCenter;
        cell.detailTextLabel.textColor = [UIColor blackColor];
    }else {
        UIImageView *contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(cell.frame), 200)];
        NSString * string=[NSString stringWithFormat:@"http://mall.yunku.mobi:8080/mall/upload/goods/%@",_dataSource[indexPath.row]];
        NSURL * url=[NSURL URLWithString:string];
        
        [contentImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"背景.png"]];
        [cell addSubview:contentImage];
        
//        UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(contentImage.frame) + 5, CGRectGetWidth(cell.frame), 40)];
    }
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_CellType == KJParameterType) {
        return 40;
    }else {
        return 200;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
